package com.example.demo.game;

import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author: 啵啵虎
 * @Date: 2024-05-06  17:07
 */

@Component
public class OnlineUserManager {

    //这个哈希表就用来表示当前用户在游戏大厅的在线状态
    private ConcurrentHashMap<Integer, WebSocketSession> gameHall=new ConcurrentHashMap<>();

    private ConcurrentHashMap<Integer,WebSocketSession> gameRoom=new ConcurrentHashMap<>();

    public void enterGameHall(int userId,WebSocketSession webSocketSession){
        gameHall.put(userId,webSocketSession);
    }

    public void exitGameHall(int userId){
        gameHall.remove(userId);
    }

    public WebSocketSession getFromGameHall(int userId){
        return gameHall.get(userId);
    }

    public void enterGameRoom(int userId,WebSocketSession webSocketSession){
        gameRoom.put(userId,webSocketSession);
    }

    public void exitGameRoom(int userId){
        gameRoom.remove(userId);
    }

    public WebSocketSession getFromGameRoom(int userId){
        return gameRoom.get(userId);
    }
}
